function setSelectedTextRange(elm, selectionStart, selectionEnd) {
	if (elm.setSelectionRange) {
		elm.focus();
		elm.setSelectionRange(selectionStart, selectionEnd);
	} else if (elm.createTextRange) {
		var range = elm.createTextRange();
		range.collapse(true);
		range.moveEnd('character', selectionEnd);
		range.moveStart('character', selectionStart);
		range.select();
	}
}

function setCaretToPos (elm, pos) {
	setSelectedTextRange(elm, pos, pos);
}

function getCaretPosition(elm) {
if (typeof elm.selectionStart != "undefined") {
	return elm.selectionStart;
} else if (document.selection) {
	return Math.abs(document.selection.createRange().moveStart("character",-1000000));
}
}


function validate_input(e) {
	var regx = /\"|\'/
	//var oggetto_attivo = document.activeElement;

	if ("activeElement" in document)
  	{ 
		oggetto_attivo = document.activeElement; //Si IE
	} else  { 
		oggetto_attivo = e ? e.explicitOriginalTarget : null;  // Si Firefox
	}
	//alert (oggetto_attivo.id);
	posizione_cursore = getCaretPosition(oggetto_attivo); 
	oggetto_attivo.value =  oggetto_attivo.value.replace(regx, "`");
	setCaretToPos (oggetto_attivo, posizione_cursore);
}

function validate() {
    var form = document.forms[0];
    var regs = "'\n\nInvalid characters: \" & '";
    //var regx = /\"|\&|'/
    var regx = /\"|\'/
    for (var i=0; i<form.elements.length; i++) {
        if (regx.test(form.elements[i].value)) {
            //alert("Invalid character(s) in '" + form.elements[i].name + regs);
            //form.elements[i].value = "";
            form.elements[i].value =  form.elements[i].value.replace(regx, "`");
            return false;
        }
       }
    return true;
}
